<?php
namespace app\lucn\controller;

use think\Request;
use think\Db;
use think\Cache;
use fast\lucn\Lucn;

/**
 * 首页控制器
 * @author  Lucn30 <Lucn30@126.com>
 */

class Index extends Base {

    protected $model = null;

    public function _initialize()
    {
		if (session('?user_auth')) {
            if (!defined('UID')) {
    		    define('UID', session('user_auth.uid'));
            }
    	} else {
    		define('UID', null);
    	}
        if(!UID){
			$this->redirect('/'.Request::instance()->module().'/login/login');
			return false;
        }
		
		define('SIGN', 'index');//定义标识
		define('DB', 'lucn30');//定义标识
		define('TABLE1', 'favorite_list');//定义标识
		define('TABLE2', 'favorite_class');//定义标识
		define('PRE', '');//定义标识
		$this->assign('controller_sign',SIGN);
		$this->assign('controller_page','lucn首页');
        parent::_initialize();
    }
	
	public function ajax_clear(){
		Cache::rm('favorite_category'); 
		Cache::rm('favorite_list'); 
		return (array('a'=>1,'b'=>'清理缓存成功'));
	}

    public function index() {
	    $this->assign('controller_sign',SIGN);
	    $this->assign('method_sign',SIGN);
		if(!$list1 = cache::get('favorite_category')){
			$where = NULL;
	    	$list1 = DB::table(DB.'.'.PRE.TABLE2)->where($where)->select();
			cache::set('favorite_category',$list1);
		}
		
		if(!$list2 = cache::get('favorite_list')){
		$list2 = array();
		    foreach($list1 as $i => $son){
		        $where['class'] = $son['id'];
		        $list2[$i] = DB::table(DB.'.'.PRE.TABLE1)->where($where)->select();
		    }  
			cache::set('favorite_list',$list2);
		}
        
		$this->assign('db',DB);
		$this->assign('tb1',TABLE1);
		$this->assign('tb2',TABLE2);
		$this->assign('list1',$list1);
		$this->assign('list2',$list2);
		
		$this->assign('title','lucn首页');
		return $this->Fetch('/nav_index');
    }
	
	public function update(){
	    $db = input('param.db');
		$tb = input('param.tb');
		$post= input('param.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id' && !empty($son)){
		    $data[$son] = $post[$son];
			}
		}
		$where['id'] = $post['id'];
		$r = DB::table($db.'.'.PRE.$tb)->where($where)->update($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "修改成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".DB::table($db.'.'.PRE.$tb)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		return (array('a'=>$a,'b'=>$b));
	}
	
	public function delete(){
	    $db = input('param.db');
		$tb = input('param.tb');
		$post= input('param.');
		if($db && $tb){
		$where['id'] = $post['id'];
		$r = DB::table($db.'.'.PRE.$tb)->where($where)->delete();
		//dump($where);die;
		if($r){
		    $a = 1;$b = "删除成功";}else{$a = 0;$b = "删除失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".DB::table($db.'.'.PRE.$tb)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$post['id']."<br>";}
		return (array('a'=>$a,'b'=>$b));
	}
	
	public function delete_class(){
	    $db = input('param.db');
		$tb = input('param.tb');
		$post= input('param.');
		if($db && $tb){
	    //先检测本类下是否为空
		$map['class'] = $post['id'];
		$count = DB::table($db.'.'.PRE.TABLE1)->where($map)->count();
		if($count<1){
		    $where['id'] = $post['id'];
		    $r = DB::table($db.'.'.PRE.$tb)->where($where)->delete();
		}else{
			$r = 0; $a=0;$c='分类下不为空';
		}
		//dump($where);die;
		if($r){
		    $a = 1;$b = "删除分类成功";
		}else{
			$c = isset($c) ? $c : '删除分类失败';
			$a = 0;$b = $c."。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$post['id']."<br>SQL:".DB::table($db.'.'.PRE.$tb)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		return (array('a'=>$a,'b'=>$b));
	}
	
	public function update_class(){
	    $db = input('param.db');
		$tb = input('param.tb');
		$post= input('param.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id' && !empty($son)){
		    $data[$son] = $post[$son];
			}
		}
		$where['id'] = $post['id'];
		$r = DB::table($db.'.'.PRE.$tb)->where($where)->update($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "修改成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".DB::table($db.'.'.PRE.$tb)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		return (array('a'=>$a,'b'=>$b));
	}
	
	public function insert(){
	    $db = input('param.db');
		$tb = input('param.tb');
		$post= input('param.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		//dump($arr);die;
		foreach($arr as $i => $son){
		   if($son != 'id' && !empty($son)){
		    $data[$son] = $post[$son];
			}
		}
		
		unset($data['id']);
		$r = DB::table($db.'.'.PRE.$tb)->insert($data);
		if($r){
		    $a = 1;$b = "添加成功";}else{$a = 0;$b = "insert数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>SQL:".DB::table($db.'.'.PRE.$tb)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$id."<br>";}
		return (array('a'=>$a,'b'=>$b));
	}
	
	public function insert_class(){
	    $db = input('param.db');
		$tb = input('param.tb');
		$post= input('param.');
		if($db && $tb){
		$arr = explode('|',$post['field']);$i=0;
		foreach($arr as $i => $son){
		   if($son != 'id' && !empty($son)){
		    $data[$son] = $post[$son];
			}
		}
		unset($data['id']);
		$r = DB::table($db.'.'.PRE.$tb)->insert($data);
		//dump($where);die;
		if($r){
		    $a = 1;$b = "添加成功";}else{$a = 0;$b = "update数据失败。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>SQL:".DB::table($db.'.'.PRE.$tb)->getLastSql();
		}

        }else{$a = 0;$b = "库和表不能为空。<br>DB:".$db."；<br>TB:".$tb."；<br>ID:".$date['id']."<br>";}
		return (array('a'=>$a,'b'=>$b));
	}
}